package com.taotao.rest.controller;

import com.taotao.rest.pojo.CatResult;
import com.taotao.rest.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by new on 2018/8/15.
 */
@Controller
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    /**
     * jsonp访问方式1
     * @param callback
     * @return
     */
    //解决乱码
//    @RequestMapping(value = "itemcat/list",produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
//    @ResponseBody
//    public String getItemCatList(String callback){
//        CatResult itemCatList = itemCatService.getItemCatList();
//        String json = JsonUtils.objectToJson(itemCatList);
//        //拼装返回值
//        String result = callback + "(" + json + ");";
//        return result;
//
//    }

    /**
     * jsonp访问方式2,spring4.1
     * @param callback
     * @return
     */
    @RequestMapping(value = "itemcat/list",produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    @ResponseBody
    public Object getItemCatList(String callback){
        CatResult itemCatList = itemCatService.getItemCatList();
        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(itemCatList);
        mappingJacksonValue.setJsonpFunction(callback);
        return mappingJacksonValue;

    }
}
